﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Server";

            //1
            Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //2
            //IPAddress [] addresses = Dns.GetHostAddresses(Dns.GetHostName());
            //foreach (IPAddress address in addresses)
            //{
            //    Console.WriteLine("address: {0}", address.Address);
            //}

            //IPHostEntry address = Dns.GetHostByAddress("10.7.9.12");
            //Console.WriteLine("hostName: {0}; ", address.HostName);
            //foreach (var s in address.AddressList)
            //{
            //    Console.WriteLine(s.Address.ToString());
            //}

            socket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345));

            //3.
            socket.Listen(10);

            //4
            Console.WriteLine("I`m socket, I`m waiting for clients");
            Socket client = socket.Accept();

            IPEndPoint point = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("RemoteEndPoint: {0}; {1}", point.Address, point.Port);

            IPEndPoint point2 = (IPEndPoint)client.LocalEndPoint;

            Console.WriteLine("LocalEndPoint: {0}; {1}", point2.Address, point2.Port);
            Console.WriteLine("I waited for client, I`m waiting for message");

            byte[] buffer = new byte[255];
            int count = client.Receive(buffer);
            Console.WriteLine(ASCIIEncoding.ASCII.GetString(buffer));

            client.Send(ASCIIEncoding.ASCII.GetBytes("Hello, client!!!"));


            client.Shutdown( SocketShutdown.Both);
            //socket.Shutdown( SocketShutdown.Receive);

            client.Close();
            socket.Close();



        }
    }
}
